// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var html_createBadassetsHandle = require("./html_createBadassetsHandle.html");
	var css_financeManage = require("../financing/financingManage.css");
	var css_financeDetailInfo = require("../financing/financingDetailInfo.css");
	var css_badassets = require("./badassetsHandleList.css");
	var js_platform_main = require("./platform_main.js");
	var js_exception = require("../exception/exceptionManage");
	var dialog = require("dialog");
	var tools = require("tools");
	var validate = require("validate");
	var constant = require("constant")
	var databind = require("databind");
	var count = 0;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var left;
	var tm;
	var financeState;
	var financeId;
	var financeNumber;
	var dataFinancingDetail;
	var badassetsHandleId;
	var init = function(page) {
		left = page.params.left || base.getUrlParam("left");
		financeState = page.params.financeState || base.getUrlParam("financeState");
		tm = page.params.tm || base.getUrlParam("tm");
		$(".left").removeClass("none");
		leftMenu.init(6);
		if(tm!=null){
			js_exception.init();
			topMenu.init(5);
			inquireBulkstockInventoryPageList1();
		}else{
			leftMenu.init(6);
			js_platform_main.init(0);
			inquireBulkstockInventoryPageList();
		}
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		$("#main").css("background", "#fff");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu7").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		$("#leftMenu7 .leftMenu-disable").removeClass("leftMenu-open");
		$("#leftMenu7 .leftMenu-disable:last-child").addClass("leftMenu-open");
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		$("#handleButton").unbind("click").click(function(){
			if($(".badassetsCheckeds:checked").length <= 0){
				base.alert(2, "请选择要处置的货箱！");
				return;
			}
			
			$("#popdialog").html(html_createBadassetsHandle);
			tools.inquireCommonSelectorList(9, $("#create_badassets_handle_dialog .handleReson"));
			dialog.showDialog($("#create_badassets_handle_dialog"));
			$("#create_badassets_handle_dialog .inventoryIds").html("");
			$.each($(".badassetsCheckeds:checked"), function() {
				var str = "<input type='hidden' value='"+ $(this).parents("tr").attr("inventoryId") +"' name='inventoryId'>";
				$("#create_badassets_handle_dialog .inventoryIds").append(str);
			});
			
			$("#create_badassets_handle_dialog .sure_no").unbind("click").click(function(){
				dialog.hideDialog($("#create_badassets_handle_dialog"));
			});
			
			$("#create_badassets_handle_dialog .sure_yes").unbind("click").click(function(){
				databind.submitDom($("#create_badassets_handle_dialog"));
			});
		});
		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});
		
		$("#badassetsHandleList .searchButon").unbind("click").click(function() {
			if ($("#badassetsHandleList .searchStatus").val() == 1) {
				inquireBadassetsHandlePageList1();
			} else {
				inquireBadassetsHandlePageList();
			}
		});
	};

	var inquireBulkstockInventoryPageList = function() {
		base.fnAjax({
			action: "inquireBulkstockInventoryPageList.json",
			oData: {
				inventoryId: $("#badassetses").val().split(','),
//				inventoryId: [575,576,577,578,574],
				pageIndex: pageIndex,
				pageSize: pageSize,
				enterpriseName: $("#badassetsHandleList .enterpriseName").val(),
				financeNumber: $("#badassetsHandleList .financeNumber").val()
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#badassetsHandleListTpl").html();
					var html = juicer(tpl, data);
					$("#badassetsHandleListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#badassetsHandlePagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockInventoryPageList();
						});
						$(".pickupRansomDetail").unbind("click").click(function(){
							router.route("main/pickupRansomDetail.html?pickupId="+$(this).parents("tr").attr("pickupId"));
						});
					$(".badassetsCheckeds").unbind("click").click(function(){
						count = $(".badassetsCheckeds:checked").length;
						$("#handleButton span").html(count);
					});
					
					$(".detail").unbind("click").click(function(){
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
					
					$(".handleBad").unbind("click").click(function(){
						$("#popdialog").html(html_createBadassetsHandle);
						tools.inquireCommonSelectorList(9, $("#create_badassets_handle_dialog .handleReson"));
						dialog.showDialog($("#create_badassets_handle_dialog"));
						$("#create_badassets_handle_dialog .inventoryIds").html("");
						var str = "<input type='hidden' value='"+ $(this).parents("tr").attr("inventoryId") +"' name='inventoryId'>";
						$("#create_badassets_handle_dialog .inventoryIds").append(str);
						
						$("#create_badassets_handle_dialog .sure_no").unbind("click").click(function(){
							dialog.hideDialog($("#create_badassets_handle_dialog"));
						});
						
						$("#create_badassets_handle_dialog .sure_yes").unbind("click").click(function(){
							databind.submitDom($("#create_badassets_handle_dialog"));
						});
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	var inquireBulkstockInventoryPageList1 = function() {
		base.fnAjax({
			action: "inquireBulkstockInventoryPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				enterpriseName: $("#badassetsHandleList .enterpriseName").val(),
				financeNumber: $("#badassetsHandleList .financeNumber").val()
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#badassetsHandleListTpl").html();
					var html = juicer(tpl, data);
					$("#badassetsHandleListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#badassetsHandlePagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockInventoryPageList1();
						});
						$(".pickupRansomDetail").unbind("click").click(function(){
							router.route("main/pickupRansomDetail.html?pickupId="+$(this).parents("tr").attr("pickupId"));
						});
					$(".badassetsCheckeds").unbind("click").click(function(){
						count = $(".badassetsCheckeds:checked").length;
						$("#handleButton span").html(count);
					});
					
					$(".detail").unbind("click").click(function(){
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
					
					$(".handleBad").unbind("click").click(function(){
						$("#popdialog").html(html_createBadassetsHandle);
						tools.inquireCommonSelectorList(9, $("#create_badassets_handle_dialog .handleReson"));
						dialog.showDialog($("#create_badassets_handle_dialog"));
						$("#create_badassets_handle_dialog .inventoryIds").html("");
						var str = "<input type='hidden' value='"+ $(this).parents("tr").attr("inventoryId") +"' name='inventoryId'>";
						$("#create_badassets_handle_dialog .inventoryIds").append(str);
						
						$("#create_badassets_handle_dialog .sure_no").unbind("click").click(function(){
							dialog.hideDialog($("#create_badassets_handle_dialog"));
						});
						
						$("#create_badassets_handle_dialog .sure_yes").unbind("click").click(function(){
							databind.submitDom($("#create_badassets_handle_dialog"));
						});
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	return {
		init: init
	};
});